package com.jack.helloworld.util;

import java.text.DecimalFormat;

public class MyEclipseKeyGen {

    private int MAX_LENGTH = 6;

    public String getSerial(String userId) {
        if (userId == null || userId.length() == 0) {
            return null;
        }
        if (userId.length() < MAX_LENGTH) {
            String addUserId = userId + "000000";
            userId = addUserId.substring(0, MAX_LENGTH);
        }
        int suf = this.decode(userId.substring(0, 1));
        String dc = new DecimalFormat("000").format(suf);
        String code = new StringBuilder(userId.substring(1, MAX_LENGTH)).append(dc).toString();
        return this.change(code);
    }

    private int decode(String s) {
        int i;
        char[] ac;
        int j;
        int k;
        i = 0;
        ac = s.toCharArray();
        j = 0;
        k = ac.length;
        while (j < k) {
            i = (31 * i) + ac[j];
            j++;
        }
        return Math.abs(i);
    }

    private String change(String s) {
        byte[] abyte0;
        char[] ac;
        int i;
        int k;
        int j;
        abyte0 = s.getBytes();
        ac = new char[s.length()];
        i = 0;
        k = abyte0.length;
        while (i < k) {
            j = abyte0[i];
            if ((j >= 48) && (j <= 57)) {
                j = (((j - 48) + 5) % 10) + 48;
            } else if ((j >= 65) && (j <= 90)) {
                j = (((j - 65) + 13) % 26) + 65;
            } else if ((j >= 97) && (j <= 122)) {
                j = (((j - 97) + 13) % 26) + 97;
            }
            ac[i] = (char) j;
            i++;
        }
        return String.valueOf(ac);
    }

    public static void main(String[] args) {
        for (int index = 0; index < 2; index++) {
            String userId = index == 0 ? "A65I1H" : "A65I1H";
            MyEclipseKeyGen myeclipsekeygen = new MyEclipseKeyGen();
            String res = myeclipsekeygen.getSerial(userId);
            System.out.println("Serial:" + res);
        }
    }
}
